/*
 * Adapted from cppreference - https://en.cppreference.com/w/cpp/numeric/midpoint
 * */
#include <cxxomfort/base.hpp>
#include <cxxomfort/numeric.hpp>
#include <cxxomfort/library/numeric.hpp>
#include <cxxomfort/cstdint.hpp>
#include <cxxomfort/limits.hpp>
#include <iostream>

namespace numeric= cxxomfort::library::numeric;

void on_pointers (int i, int j) {
	using namespace std;
	char const* text = "0123456789";
	char const* p = text + i;
	char const* q = text + j;
	cout << "std::midpoint('" << *p << "', '" << *q << "'): '"
			  << *midpoint(p, q) << "'\n";
};
 
 
int main() {
	using namespace std;
	cout<< "std::midpoint"<< endl;
	enum { R = 8 };
	uint32_t const a = numeric_limits<uint32_t>::max();
	uint32_t const b = numeric_limits<uint32_t>::max() - R;
    
    cout<< "a: " << a << '\n'
		<< "b: " << b << '\n'
		<< "Incorrect (overflow and wrapping): " << (a + b) / 2 << '\n'
		<< "Still incorrect (+split-the-diff): " << (a + (b-a)/2)<< '\n'
		<< "Correct   (calling std::midpoint): " << midpoint(a, b) << "\n\n";
 
    on_pointers(2, 4);
    on_pointers(2, 5);
    on_pointers(5, 2);
    on_pointers(2, 6);
    
    cout<< "\ncxxomfort template helper st_math::midpoint"<< endl;
    cout<< endl;
    cout<< "calling ctmath::midpoint: "<< numeric::ctmath::midpoint<uint32_t, ULONG_MAX, ULONG_MAX-R>::value<< endl;
    cout<< endl;
    cout<< numeric::ctmath::midpoint<int, 2, 4>::value<< endl;
    cout<< numeric::ctmath::midpoint<int, 2, 5>::value<< endl;
    cout<< numeric::ctmath::midpoint<int, 5, 2>::value<< endl;
    cout<< numeric::ctmath::midpoint<int, 2, 6>::value<< endl;
}
